//
//  CollectionTableCell.m
//  YelenaPackaging
//
//  Created by rzhyios on 2018/8/23.
//  Copyright © 2018年 Lemon. All rights reserved.
//

#import "CollectionTableCell.h"
#import "CollectionImageCell.h"
#import "WebViewController.h"
#import "Config.h"
#import "ShanDongTool.h"

#define XHScreenWidth [UIScreen mainScreen].bounds.size.width
#define XHScreenHeight [UIScreen mainScreen].bounds.size.height
@interface CollectionTableCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate, UIImagePickerControllerDelegate,CollectionImageCellDelegate>
@property (strong, nonatomic) NSMutableArray * mutImage;
@property (strong, nonatomic) NSArray *titleArray;
@end
@implementation CollectionTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUI];
    self.mutImage = [[NSMutableArray alloc]init];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setUI{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection=UICollectionViewScrollDirectionVertical;
    layout.itemSize=CGSizeMake(100, 90);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = (XHScreenWidth-300)/6;
    
    
    _collection.backgroundColor=[UIColor whiteColor];
    _collection.collectionViewLayout=layout;
    [_collection registerNib:[UINib nibWithNibName:@"CollectionImageCell"bundle:nil]forCellWithReuseIdentifier:@"CollectionImageCell"];
    _collection.delegate=self;
    _collection.dataSource=self;
    self.titleArray = @[@"电子健康卡",@"找医院",@"找医生",@"云急救",@"环境健康",@"云诊室"];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

      return  self.titleArray.count;
 
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionImageCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionImageCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.ylIndexPath = indexPath;
    cell.iconImage.image =[UIImage imageNamed:self.titleArray[indexPath.row]] ;
    cell.titleLabel.text = self.titleArray[indexPath.row];
    return cell;
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSString *)base64EncodeString:(NSString *)string
{
    //1.先转换为二进制数据
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    //2.对二进制数据进行base64编码,完成之后返回字符串
    return [data base64EncodedStringWithOptions:0];
}

-(void)pushVCWithTitle:(NSString*)title code:(NSString*)code path:(NSString*)path{
    WebViewController * vc = [[WebViewController alloc]init];
    NSString *url = [NSString stringWithFormat:@"%@?code=%@",path,code];
    vc.url =  [URLTool webURLStringNoBlankWithPathString:url token:nil];
    vc.title = title;
    [self.previousVC.navigationController pushViewController:vc animated:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.item);
    switch (indexPath.row) {
        case 0:
        {
            [self goDzjkk];
        }
            break;
        case 1:
        {
            [self goHospital];
        }
            break;
        case 2:
        {
             [self goDoctor];
        }
            break;
        case 3:
        {
            [self goAid];
        }
            break;
        case 4:
        {
             [self goHuanjing];
        }
            break;
        case 5:
        {
            [self goYzs];
        }
            break;
        default:
            break;
    }
}
-(void)ylDelImageBtnClick:(UIButton *)button indexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.item);
  
}

-(void)goDzjkk{
    NSDictionary *param = [ShanDongTool paramWithBody:@"dzjkk"
                                                 name:@"Yelena"
                                               idCard:@"350104199110044023"
                                                phone:@"18050430827"
                                                  sex:@"0"];
    __weak __typeof__(self) weakSelf = self;
    [HTTPSTool POSTRequestWithPath:ShanDong_GetCode_URL params:param progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSDictionary * _Nonnull JSON) {
        NSLog(@"%@",JSON);
        // Body
        if ([JSON[@"data"] isKindOfClass:[NSDictionary class]]) {
            NSString *code = JSON[@"data"][@"code"];
            [weakSelf pushVCWithTitle:@"电子健康卡"
                                 code:code
                                 path:@"https://yjt.sddingkao.com/web/module/common/dist/#/ownTwo"];
        }
        // NoBody
        else if ([JSON[@"data"] isKindOfClass:[NSString class]]){
            NSString *code = JSON[@"data"];
            [weakSelf pushVCWithTitle:@"电子健康卡"
                                 code:code
                                 path:@"https://yjt.sddingkao.com/web/module/common/dist/#/ownTwo"];
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

-(void)goHospital{
    NSDictionary *param = [ShanDongTool paramNoBody:@"hospital"];
    __weak __typeof__(self) weakSelf = self;
    [HTTPSTool POSTRequestWithPath:ShanDong_GetCode_URL params:param progress:^(NSProgress * _Nonnull progress) {
    } success:^(NSDictionary * _Nonnull JSON) {
        NSLog(@"%@",JSON);
        // NoBody
        if ([JSON[@"data"] isKindOfClass:[NSString class]]){
            NSString *code = JSON[@"data"];
            [weakSelf pushVCWithTitle:@"找医院"
                                 code:code
                                 path:@"https://yjt.sddingkao.com/web/module/common/hospital/"];
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

-(void)goDoctor{
    NSDictionary *param = [ShanDongTool paramNoBody:@"doctor"];
    __weak __typeof__(self) weakSelf = self;
    [HTTPSTool POSTRequestWithPath:ShanDong_GetCode_URL params:param progress:^(NSProgress * _Nonnull progress) {
    } success:^(NSDictionary * _Nonnull JSON) {
        NSLog(@"%@",JSON);
        // NoBody
        if ([JSON[@"data"] isKindOfClass:[NSString class]]){
            NSString *code = JSON[@"data"];
            [weakSelf pushVCWithTitle:@"找医生"
                                 code:code
                                 path:@"https://yjt.sddingkao.com/web/module/common/dist/#/doctor"];
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

-(void)goAid{
    NSDictionary *param = [ShanDongTool paramNoBody:@"aid"];
    __weak __typeof__(self) weakSelf = self;
    [HTTPSTool POSTRequestWithPath:ShanDong_GetCode_URL params:param progress:^(NSProgress * _Nonnull progress) {
    } success:^(NSDictionary * _Nonnull JSON) {
        NSLog(@"%@",JSON);
        // NoBody
        if ([JSON[@"data"] isKindOfClass:[NSString class]]){
            NSString *code = JSON[@"data"];
            [weakSelf pushVCWithTitle:@"云急救"
                                 code:code
                                 path:@"https://yjt.sddingkao.com/web/module/common/dist/#/aid"];
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

-(void)goHuanjing{
    NSDictionary *param = [ShanDongTool paramNoBody:@"huanjing"];
    __weak __typeof__(self) weakSelf = self;
    [HTTPSTool POSTRequestWithPath:ShanDong_GetCode_URL params:param progress:^(NSProgress * _Nonnull progress) {
    } success:^(NSDictionary * _Nonnull JSON) {
        NSLog(@"%@",JSON);
        // NoBody
        if ([JSON[@"data"] isKindOfClass:[NSString class]]){
            NSString *code = JSON[@"data"];
            [weakSelf pushVCWithTitle:@"环境健康"
                                 code:code
                                 path:@"https://yjt.sddingkao.com/web/module/common/huanjing/"];
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

-(void)goYzs{
    NSDictionary *param = [ShanDongTool paramNoBody:@"yzs"];
    __weak __typeof__(self) weakSelf = self;
    [HTTPSTool POSTRequestWithPath:ShanDong_GetCode_URL params:param progress:^(NSProgress * _Nonnull progress) {
    } success:^(NSDictionary * _Nonnull JSON) {
        NSLog(@"%@",JSON);
        // NoBody
        if ([JSON[@"data"] isKindOfClass:[NSString class]]){
            NSString *code = JSON[@"data"];
            [weakSelf pushVCWithTitle:@"云诊室"
                                 code:code
                                 path:@"https://yjt.sddingkao.com/web/module/common/dist/#/yzs"];
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}


@end
